#!/bin/bash
# 安装桌面快捷方式脚本

echo "🎹 安装会说话的键盘桌面快捷方式..."

CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 检查并安装依赖
echo "🔍 检查系统依赖..."

# 检查Python3
if ! command -v python3 &> /dev/null; then
    echo "❌ 未找到Python3，正在安装..."
    if command -v apt &> /dev/null; then
        sudo apt update && sudo apt install -y python3 python3-pip
    elif command -v yum &> /dev/null; then
        sudo yum install -y python3 python3-pip
    elif command -v dnf &> /dev/null; then
        sudo dnf install -y python3 python3-pip
    elif command -v pacman &> /dev/null; then
        sudo pacman -S --noconfirm python python-pip
    else
        echo "❌ 无法自动安装Python3，请手动安装"
        exit 1
    fi
    echo "✅ Python3安装完成"
fi

# 检查pip
if ! command -v pip3 &> /dev/null; then
    echo "❌ 未找到pip3，正在安装..."
    if command -v apt &> /dev/null; then
        sudo apt install -y python3-pip
    elif command -v yum &> /dev/null; then
        sudo yum install -y python3-pip
    elif command -v dnf &> /dev/null; then
        sudo dnf install -y python3-pip
    elif command -v pacman &> /dev/null; then
        sudo pacman -S --noconfirm python-pip
    fi
    echo "✅ pip3安装完成"
fi

# 检查系统音频库
echo "🔊 检查音频库..."
if command -v apt &> /dev/null; then
    # Ubuntu/Debian系统
    sudo apt update
    sudo apt install -y python3-dev libasound2-dev portaudio19-dev
    echo "✅ 音频库安装完成"
elif command -v yum &> /dev/null; then
    # CentOS/RHEL系统
    sudo yum install -y python3-devel alsa-lib-devel portaudio-devel
    echo "✅ 音频库安装完成"
elif command -v dnf &> /dev/null; then
    # Fedora系统
    sudo dnf install -y python3-devel alsa-lib-devel portaudio-devel
    echo "✅ 音频库安装完成"
elif command -v pacman &> /dev/null; then
    # Arch系统
    sudo pacman -S --noconfirm python portaudio alsa-lib
    echo "✅ 音频库安装完成"
fi

# 检查Python依赖库
echo "🐍 检查Python依赖库..."
python3 -c "import pynput, pygame" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "📦 安装Python依赖库..."
    pip3 install --user pynput pygame
    if [ $? -ne 0 ]; then
        echo "❌ 依赖库安装失败，尝试使用sudo..."
        sudo pip3 install pynput pygame
        if [ $? -ne 0 ]; then
            echo "❌ 依赖库安装失败，请手动运行: pip3 install pynput pygame"
            echo "或者: sudo pip3 install pynput pygame"
            exit 1
        fi
    fi
    echo "✅ Python依赖库安装完成"
else
    echo "✅ Python依赖库已存在"
fi

# 检查音频文件
echo "🎵 检查音频文件..."
if [ ! -d "$CURRENT_DIR/audio" ]; then
    echo "❌ 未找到audio目录"
    exit 1
fi

AUDIO_COUNT=$(find "$CURRENT_DIR/audio" -name "*.mp3" -o -name "*.wav" | wc -l)
if [ $AUDIO_COUNT -eq 0 ]; then
    echo "⚠️ 未找到音频文件，程序可能无法正常工作"
else
    echo "✅ 找到 $AUDIO_COUNT 个音频文件"
fi

DESKTOP_DIR="$HOME/Desktop"
if [ ! -d "$DESKTOP_DIR" ]; then
    DESKTOP_DIR="$HOME/桌面"
    if [ ! -d "$DESKTOP_DIR" ]; then
        echo "❌ 未找到桌面目录"
        exit 1
    fi
fi


APPLICATIONS_DIR="$HOME/.local/share/applications"
mkdir -p "$APPLICATIONS_DIR"

cat > "$APPLICATIONS_DIR/speaking-keyboard.desktop" << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=Speaking Keyboard
Comment=A humorous keyboard taunt program optimized for Linux systems
Exec=$CURRENT_DIR/start_keyboard.sh
Icon=input-keyboard
Path=$CURRENT_DIR
Terminal=true
StartupNotify=true
Categories=AudioVideo;Audio;Utility;
Keywords=keyboard;sound;audio;fun;entertainment;
MimeType=
StartupWMClass=speaking-keyboard
EOF

if [ $? -eq 0 ]; then
    echo "✅ 应用程序菜单项已添加（英文版）"
fi


# 更新桌面数据库
if command -v update-desktop-database &> /dev/null; then
    update-desktop-database "$APPLICATIONS_DIR"
    echo "✅ 桌面数据库已更新"
fi
 
echo ""
echo "🎉 安装完成！"
echo "📁 桌面快捷方式位置: $DESKTOP_DIR/speaking-keyboard.desktop"
echo "🎮 现在可以通过以下方式启动程序："
echo "   • 双击桌面上的'会说话的键盘'图标"
echo "   • 在应用程序菜单中搜索'会说话的键盘'或'Speaking Keyboard'"
echo ""
echo "✅ 已自动安装的依赖："
echo "   • Python3 和 pip3"
echo "   • 系统音频库"
echo "   • Python依赖库 (pynput, pygame)"
echo "   • 找到 $AUDIO_COUNT 个音频文件"
echo ""
echo "💡 使用说明:"
echo "   1. 双击桌面上的'会说话的键盘'图标"
echo "   2. 程序会在终端中运行"
echo "   3. 按任意支持的按键测试音效"
echo "   4. 按 Ctrl+C 退出程序"
echo ""
echo "🔧 支持的按键:"
echo "   • 基础键: Tab, Enter, 空格, 删除键, F5, CapsLock"
echo "   • 组合键: Ctrl+S, Ctrl+C, Ctrl+V, Ctrl+Z, Ctrl+F, Ctrl+Q, Alt+F4, Ctrl+Alt+T"
echo "   • 游戏键: Q, W, E, R (盖伦技能)"
echo ""
echo "🎵 音频文件位置: $CURRENT_DIR/audio/"
